Skip to content

fix: honour Accept-Encoding quality values and set Vary - #28

Merged
CodeDredd merged 1 commit into
mainfrom
fix/accept-encoding-negotiation
Nov 24, 2026
Merged

CodeDredd merged 1 commit into
mainfrom
fix/accept-encoding-negotiation

Conversation

@CodeDredd

Copy link
Copy Markdown
Owner

Closes #25

What

1. RFC 9110 Accept-Encoding parsing

Accept-Encoding used to be matched with header.includes(method), so quality values were ignored entirely. It is now parsed per RFC 9110 §12.5.3:

Accept-Encoding before after
br;q=0, gzip br gzip
gzip;q=0 gzip —
gzip;q=0.1, deflate;q=0.9 gzip deflate
br;q=0, * — gzip
x-gzip — gzip
deflate-raw deflate —
  • The client's quality values decide; q=0 (or a malformed q) rejects a coding.
  • Server preference (zstd → br → gzip → deflate, opt-ins unchanged) only breaks ties.
  • * covers every coding not listed; coding names are case-insensitive.
  • No header → no compression (unchanged).

The forced composables (useGZipCompression, …) and compress(event, response, 'gzip') use the same check, so a forced coding the client rejected with q=0 is no longer applied.

2. Vary: Accept-Encoding

Every negotiated response now gets Vary: Accept-Encoding — compressed or not, since another Accept-Encoding would have changed the outcome either way. Existing values are kept (Origin → Origin, Accept-Encoding), it's never duplicated and Vary: * is left alone. On the hook path a Vary in the render:response header record is updated in place, because Nitro applies that record after the hook.

Not varied: already-encoded responses, empty bodies, bodies below minSize/threshold, and a method forced on the middleware — that one is documented as "always", ignores Accept-Encoding, and stays that way.

3. Plumbing

compress() / compressStream() take method as optional now and negotiate when it's omitted (the middleware helpers already worked that way). useCompression* and the Nuxt module go through that, which is what lets them set Vary when no coding is acceptable. compressStream() also gained the options parameter. Both are backwards compatible.

Side fixes found while testing

  • Empty bodies are skipped everywhere. The compression() middleware crashed on 204 responses (TypeError: Response constructor: Invalid response status code 204), and compressionStream() gzipped the empty body h3 v2 produces for a null handler result. compressStream() crashed on an undefined body (Cannot read properties of null (reading 'pipeThrough')).
  • compressStream() with a rejected coding no longer replaces the body with a stream.

Tests

New test/negotiation.test.ts (31 of its tests fail on main):

  • table tests for getAnyCompression / getStreamCompression incl. wildcard, aliases, malformed q, case/whitespace, zstd;
  • Vary on the hook path (compressed, rejected, existing values, *, render:response record, encoded/empty/small → untouched, stream path, Nuxt plugin);
  • the issue's reproduction on both h3 v2 middlewares, plus Vary preservation, forced method, null and 204 handlers;
  • h3 v1 app hook via supertest for useCompression, useCompressionStream and the Nuxt plugin.

Full suite green locally on h3 2.0.8 and 1.15.11. README gained a "Content negotiation" section.

`Accept-Encoding` was matched by substring, so quality values were ignored:
`br;q=0, gzip` still picked brotli, `gzip;q=0` still compressed with gzip
and `gzip;q=0.1, deflate;q=0.9` picked gzip. The header is now parsed per
RFC 9110: the client's quality values decide, `q=0` rejects a coding, `*`
covers unlisted codings and `x-gzip` counts as gzip. Server preference only
breaks ties.

Every negotiated response now gets `Vary: Accept-Encoding`, compressed or
not, keeping existing `Vary` values (and updating the `render:response`
header record in place), so shared caches no longer hand a compressed body
to clients that didn't ask for it.

`compress()` / `compressStream()` negotiate when `method` is omitted, which
lets `useCompression*` and the Nuxt module set `Vary` even when no encoding
is acceptable. Empty bodies are skipped on all paths, which also stops the
middlewares from crashing on 204 responses and from gzipping empty bodies.

Closes #25
@CodeDredd
CodeDredd merged commit 1e570bd into main Nov 24, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Accept-Encoding q-values are ignored (q=0 still selected) and Vary: Accept-Encoding is not set

1 participant